//
//  HJVisitorView.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/21.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit

///1.定义协议
@objc protocol HJVisitorViewDelegate: NSObjectProtocol{
    
    ///2.定义方法
    //注册按钮被点击
    optional func visitorViewDidClickRegisterBtn()
    
    //登陆按钮被点击
    optional func visitorViewDidClickLoginBtn()
    
}

class HJVisitorView: UIView {
    ///3.定义代理属性
    weak var delegate: HJVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///设置访问视图
    func setupVisitorView(imageName: String, message: String){
        
        homeView.hidden = true
        
        //coverView.hidden = true
        ///父控件将子控件推到最下层
        self.sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName);
        messageLabel.text = message
        
        
    }
    
    func rotation(){
        
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.repeatCount = MAXFLOAT
        rotation.toValue = M_PI * 2
        rotation.duration = 20
        
        ///完成的时候不要移除
        rotation.removedOnCompletion = false
        
        ///添加到图层上
        iconView.layer.addAnimation(rotation, forKey: nil)
        
    }
    
    //MARK: -按钮点击事件
    ///注册点击事件
 @objc private func DidClickRegisterBtn(button: UIButton){
        ///4.调用代理方法
        delegate?.visitorViewDidClickRegisterBtn?()
    
    }
    
    ///登陆点击事件
    
     func DidClickLoginBtn(){
        
        delegate?.visitorViewDidClickLoginBtn?()

        
    }
    
    private func prepareUI() {
        ///背景颜色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        ///添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        ///添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints =
        false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        ///转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        ///遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        ///小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        ///消息label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        ///注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100));
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        ///登陆按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 100));
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 35))
        
    }
    
    
    //MARK: -懒加载
    ///转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"));
    
    ///遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息label
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    
    ///注册按钮
    private lazy var registerBtn: UIButton = {
        
        let button = UIButton()
        
        ///设置背景图
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        ///设置字体
        button.setTitle("注册", forState: UIControlState.Normal)
        
        ///设置颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        ///添加点击事件
        button.addTarget(self, action: "DidClickRegisterBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    ///登陆按钮
    private lazy var loginBtn: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        ///添加点击事件
        button.addTarget(self, action: "DidClickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
